<template>
	<base-page>
		<view class="userlist">
			<view class="userlist-box">
				<view class="userlist-left">
					<view class="user-title">
						员工
						<text class="iconfont icongengduo1"></text>
					</view>
					<view class="user-search">
						<view class="search">
							<text class="iconfont icon31sousuo"></text>
							<input v-model="search_text" type="text" @input="search" placeholder="请输入员工名称/手机号" />
						</view>
					</view>
					<view class="user-list-wrap">
						<block v-if="list.length > 0">
							<scroll-view scroll-y="true" class="user-list-scroll all-scroll" @scrolltolower="getUserList">
								<view class="item" @click="userSelect(item, index)" v-for="(item, index) in list" :key="index" :class="index == selectUserKeys ? 'itemhover' : ''">
									<image :src="$util.img(defaultImg.head)" @error="imgError(index)" mode="aspectFit"></image>
									<view class="item-right">
										<view>
											<view class="user-name">{{ item.username }}</view>
											<view class="user-money">{{ item.group_name }}</view>
										</view>
										<view>
											<view class="user-status">{{ item.status ? '正常' : '锁定' }}</view>
											<view class="login-time">{{ item.login_time ? $util.timeFormat(item.login_time) : '--' }}</view>
										</view>
									</view>
								</view>
							</scroll-view>
						</block>
						<view class="notYet" v-else-if="!one_judge && list.length == 0">暂无员工</view>
					</view>
					<view class="add-user"><button type="default" class="primary-btn" @click="addUser">添加员工</button></view>
				</view>
				<view class="userlist-right">
					<view class="user-title">员工详情</view>
					<view class="user-information">
						<block v-if="JSON.stringify(detail) != '{}'">
							<view class="title">基本信息</view>
							<view class="information-box">
								<view class="box-left">
									<view class="information">
										<view>员工名称：</view>
										<view>{{ detail.username }}</view>
									</view>
									<view class="information">
										<view>员工角色：</view>
										<view>{{ detail.group_name }}</view>
									</view>
									<view class="information">
										<view>员工状态：</view>
										<view>{{ detail.status ? '正常' : '锁定' }}</view>
									</view>
									<view class="information">
										<view>最后登录IP：</view>
										<view>{{ detail.login_ip ? detail.login_ip : '--' }}</view>
									</view>
									<view class="information">
										<view>最后登录时间：</view>
										<view>{{ detail.login_time ? $util.timeFormat(detail.login_time) : '--' }}</view>
									</view>
								</view>
								<image class="user-img" :src="$util.img(defaultImg.head)" @error="imgDetailError()" mode="widthFix"></image>
							</view>

							<view class="button-box" v-if="detail.is_admin == 0">
								<view class="button" @click="deleteUser(detail.uid)">删除</view>
								<view class="button" @click="editUserAction(detail.uid)">修改</view>
							</view>

							<view class="title">操作日志</view>
							<view><uni-table url="/cashier/storeapi/user/userlog" :cols="logCols" :option="{ uid: detail.uid }" :pagesize="7"></uni-table></view>
						</block>
						<block v-else><image class="cart-empty" :src="$util.img('upload/uniapp/cashier/cart_empty.png')" mode="widthFix"></image></block>
					</view>
				</view>

				<!-- 添加员工 -->
				<uni-popup ref="addmemberPop">
					<view class="pop-box addmemberPop-box">
						<view class="pop-header">
							{{ parseInt(formData.uid) > 0 ? '修改' : '添加' }}员工
							<view class="pop-header-close" @click="cancelAddUser()"><text class="iconguanbi1 iconfont"></text></view>
						</view>
						<view class="common-scrollbar pop-content">
							<view class="form-content">
								<view class="form-item">
									<view class="form-label">
										<text class="required">*</text>
										用户名：
									</view>
									<view class="form-inline search-wrap">
										<input
											type="text"
											:disabled="parseInt(formData.uid) > 0 ? true : false"
											class="form-input"
											v-model="formData.username"
											placeholder="请输入用户名"
										/>
									</view>
								</view>
								<view class="form-item" v-if="!parseInt(formData.uid)">
									<view class="form-label">
										<text class="required"></text>
										密码：
									</view>
									<view class="form-inline search-wrap"><input type="text" class="form-input" v-model="formData.password" placeholder="请输入密码" /></view>
								</view>

								<view class="form-item" v-else>
									<view class="form-label">
										<text class="required"></text>
										状态：
									</view>
									<view class="form-inline search-wrap">
										<radio-group @change="statusChange" class="form-radio-group">
											<label class="radio form-radio-item">
												<radio value="1" :checked="formData.status == 1" />
												正常
											</label>
											<label class="radio form-radio-item">
												<radio value="0" :checked="formData.status == 0" />
												锁定
											</label>
										</radio-group>
									</view>
								</view>

								<view class="form-item">
									<view class="form-label">
										<text class="required"></text>
										员工角色：
									</view>
									<view class="form-inline">
										<select-lay
											:zindex="10"
											:value="formData.group_id.toString()"
											name="names"
											placeholder="请选择员工角色"
											:options="userGroup"
											@selectitem="selectUserGroup"
										></select-lay>
									</view>
								</view>
							</view>
						</view>
						<view class="pop-bottom">
							<button type="primary" class="primary-btn" @click="save">{{ parseInt(formData.uid) > 0 ? '修改' : '添加' }}员工</button>
						</view>
					</view>
				</uni-popup>
			</view>
		</view>
	</base-page>
</template>

<script>
import unipopup from '@/components/uni-popup/uni-popup.vue';
export default {
	components: { unipopup },
	data() {
		return {
			//选中的员工下标
			selectUserKeys: 0,
			//搜索的数据
			search_text: '',
			// 初始是请求第几页
			page: 1,
			// 每次返回数据数
			page_size: 8,
			//员工列表数据
			list: [],
			// 第一次请求列表做详情渲染判断
			one_judge: true,
			//员工详情数据
			detail: {},
			logCols: [
				{
					width: 60,
					title: '操作记录',
					align: 'left',
					field: 'action_name'
				},
				{
					width: 20,
					title: '操作IP地址',
					align: 'left',
					field: 'ip'
				},
				{
					width: 20,
					title: '操作时间',
					align: 'right',
					templet: data => {
						return this.$util.timeFormat(data.create_time);
					}
				}
			],
			formData: {
				username: '',
				password: '',
				group_id: ''
			},
			userGroup: [],
			isRepeat: false
		};
	},
	onLoad() {
		// 初始化请求员工列表数据
		this.getUserList();
		this.getUserGroup();
	},
	methods: {
		switchStoreAfter() {
			this.page = 1;
			this.list = [];
			this.one_judge = true;
			this.getUserList();
		},
		// 选中的员工数据
		userSelect(item, keys) {
			this.selectUserKeys = keys;
			this.getUserDetail(item.uid);
			this.one_judge = true;
			this.isRepeat = false;
			this.formData = {
				username: '',
				password: '',
				group_id: ''
			};
		},
		statusChange(e) {
			this.formData.status = e.detail.value;
		},
		// 搜索员工
		search() {
			this.page = 1;
			this.list = [];
			this.one_judge = true;
			this.getUserList();
		},
		/**
		 * 请求的列表数据
		 */
		getUserList() {
			this.$api.sendRequest({
				url: '/cashier/storeapi/user/lists',
				data: {
					page: this.page,
					page_size: this.page_size,
					username: this.search_text
				},
				success: res => {
					if (res.data.list.length == 0 && this.one_judge) {
						this.detail = {};
						this.one_judge = false;
					}
					if (res.code >= 0 && res.data.list.length != 0) {
						this.page += 1;
						if (this.list.length == 0) {
							this.list = res.data.list;
						} else {
							this.list = this.list.concat(res.data.list);
						}

						//初始时加载一遍详情数据
						if (this.one_judge) {
							this.getUserDetail(this.list[0].uid);
						}
					}
				}
			});
		},
		imgError(index) {
			this.list[index].user_img = this.$util.img('addon/meirong/o2o/img/default_user.png');
		},
		imgDetailError() {
			this.detail.user_img = this.$util.img('addon/meirong/o2o/img/default_user.png');
		},
		getUserDetail(uid) {
			this.$api.sendRequest({
				url: '/cashier/storeapi/user/userinfo',
				data: { uid },
				success: res => {
					if (res.code == 0) {
						this.detail = res.data;
						this.one_judge = false;
					}
				}
			});
		},
		getUserGroup() {
			this.$api.sendRequest({
				url: '/cashier/storeapi/user/group',
				success: res => {
					if (res.code == 0 && res.data) {
						this.userGroup = res.data.map(item => {
							return {
								label: item.group_name,
								value: item.group_id
							};
						});
					}
				}
			});
		},
		editUserAction(uid) {
			this.$api.sendRequest({
				url: '/cashier/storeapi/user/userinfo',
				data: { uid },
				success: res => {
					if (res.code == 0) {
						this.formData = {
							username: res.data.username,
							group_id: res.data.group_id,
							uid: res.data.uid,
							status: res.data.status
						};
						this.$refs.addmemberPop.open();
					}
				}
			});
		},
		// 设置员工状态（正常：1，离职：0）
		deleteUser(uid, status) {
			this.$api.sendRequest({
				url: '/cashier/storeapi/user/deleteuser',
				data: {
					uid
				},
				success: res => {
					if (res.code >= 0) {
						this.page = 1;
						this.list = [];
						this.one_judge = true;
						this.getUserList();
					} else {
						this.$util.showToast({ title: res.message });
					}
				}
			});
		},
		addUser() {
			this.$refs.addmemberPop.open();
		},
		cancelAddUser() {
			this.formData = {
				username: '',
				password: '',
				group_id: ''
			};
			this.$refs.addmemberPop.close();
		},
		selectUserGroup(index, item) {
			if (index >= 0) {
				this.formData.group_id = parseInt(item.value);
			} else {
				this.formData.group_id = 0;
			}
		},
		save() {
			if (!this.verify() || this.isRepeat) return;
			this.isRepeat = true;
			let url = '/cashier/storeapi/user/adduser';
			if (parseInt(this.formData.uid) > 0) {
				url = '/cashier/storeapi/user/edituser';
			}

			this.$api.sendRequest({
				url: url,
				data: this.formData,
				success: res => {
					if (res.code >= 0) {
						this.$util.showToast({ title: '操作成功' });
						this.page = 1;
						this.list = [];
						this.cancelAddUser();
						this.getUserList();
						this.one_judge = true;
						this.isRepeat = false;
						this.formData = {
							username: '',
							password: '',
							group_id: ''
						};
					} else {
						this.isRepeat = false;
						this.$util.showToast({ title: res.message });
					}
				}
			});
		},
		verify() {
			if (!this.formData.username) {
				this.$util.showToast({
					title: '请输入用户名'
				});
				return false;
			}
			if (parseInt(this.formData.uid) == 0 && !this.formData.password) {
				this.$util.showToast({
					title: '请输入密码'
				});
				return false;
			}
			if (!this.formData.group_id) {
				this.$util.showToast({
					title: '请选择员工角色'
				});
				return false;
			}
			return true;
		}
	}
};
</script>

<style scoped lang="scss">
@import './public/css/user.scss';
</style>
